package com.concerneyes.health.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.concerneyes.R;
import com.concerneyes.health.model.HealthDataModel;

import java.util.HashMap;

/**
 * Created by Administrator on 2017/6/29 0029.
 */

public class HealthAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;
    String[] optometryTitle;
    private OnHealthItemClick itemClick = null;
    private HealthDataModel model;
    private boolean isAddData;
    private boolean isShowData;

    public HashMap<Integer, String> getSanGuang() {
        return sanGuang;
    }

    public void setSanGuang(HashMap<Integer, String> sanGuang) {
        this.sanGuang = sanGuang;
    }

    public HashMap<Integer, String> getJiaoZheng() {
        return jiaoZheng;
    }

    public void setJiaoZheng(HashMap<Integer, String> jiaoZheng) {
        this.jiaoZheng = jiaoZheng;
    }

    private HashMap<Integer, String> sanGuang = new HashMap<>();
    private HashMap<Integer, String> jiaoZheng = new HashMap<>();

    public HealthAdapter(Context context, OnHealthItemClick itemClick, boolean isAddData, boolean isShowData) {
        this.context = context;
        this.itemClick = itemClick;
        this.isAddData = isAddData;
        this.isShowData = isShowData;
        optometryTitle = context.getResources().getStringArray(R.array.optometry_order);
        model = new HealthDataModel();
    }

    @Override
    public int getCount() {
        return optometryTitle.length;
    }

    public void setHealthData(HealthDataModel model) {
        this.model = model;
        notifyDataSetChanged();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_optometry_order, null);
            holder.mTvOptometry1 = (EditText) convertView.findViewById(R.id.tv_optometry_1);
            holder.mTvOptometry2 = (EditText) convertView.findViewById(R.id.tv_optometry_2);
            holder.mTvOptometry3 = (EditText) convertView.findViewById(R.id.tv_optometry_3);
            holder.mTvOptometry4 = (EditText) convertView.findViewById(R.id.tv_optometry_4);
            holder.mTvOptometry5 = (EditText) convertView.findViewById(R.id.tv_optometry_5);
            holder.mView1 = (TextView) convertView.findViewById(R.id.view_1);
            holder.mView2 = (TextView) convertView.findViewById(R.id.view_2);
            holder.mView3 = (TextView) convertView.findViewById(R.id.view_3);
            holder.mView4 = (TextView) convertView.findViewById(R.id.view_4);
            holder.ll_optometry = (LinearLayout) convertView.findViewById(R.id.ll_optometry);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        setOptometryStyle(holder, position, convertView);

        if (isShowData) {
            setData(holder, position);
        }

        return convertView;
    }

    private void setData(ViewHolder holder, int position) {
        String Optometry2Text = null;
        String Optometry3Text = null;
        String Optometry4Text = null;
        String Optometry5Text = null;
        if (position == 2) {
            Optometry2Text = model.getFarRightDegree();
            Optometry3Text = model.getFarLeftDegree();
            Optometry4Text = model.getNearRightDegree();
            Optometry5Text = model.getNearLeftDegree();
        }
        if (position == 3) {
            Optometry2Text = model.getFarRightAstigmia();
            Optometry3Text = model.getFarLeftAstigmia();
            Optometry4Text = model.getNearRightAstigmia();
            Optometry5Text = model.getNearLeftAstigmia();
        }
        if (position == 4) {
            Optometry2Text = model.getFarRightAxialView();
            Optometry3Text = model.getFarLeftAxialView();
            Optometry4Text = model.getNearRightAxialView();
            Optometry5Text = model.getNearLeftAxialView();
        }
        if (position == 5) {
            Optometry2Text = model.getFarRightPupillary();
            Optometry3Text = model.getFarLeftPupillary();
            Optometry4Text = model.getNearRightPupillary();
            Optometry5Text = model.getNearLeftPupillary();
        }
        if (position == 6) {
            Optometry2Text = model.getFarRightCorrect();
            Optometry3Text = model.getFarLeftCorrect();
            Optometry4Text = model.getNearRightCorrect();
            Optometry5Text = model.getNearLeftCorrect();
        }
        if (position == 7) {
            Optometry2Text = model.getFrameSize();
            Optometry3Text = null;
            Optometry4Text = null;
            Optometry5Text = null;
        }
        if (Optometry2Text != null) {
            holder.mTvOptometry2.setText(Optometry2Text);
        }
        if (Optometry3Text != null) {
            holder.mTvOptometry3.setText(Optometry3Text);
        }
        if (Optometry4Text != null) {
            holder.mTvOptometry4.setText(Optometry4Text);
        }
        if (Optometry5Text != null) {
            holder.mTvOptometry5.setText(Optometry5Text);
        }

    }

    /**
     * 设置验光表格的样式
     *
     * @param holder
     * @param position
     */
    private void setOptometryStyle(ViewHolder holder, int position, View convertView) {
        holder.mTvOptometry1.setText(optometryTitle[position]);
        if (position == 0) {
            holder.mTvOptometry2.setGravity(Gravity.RIGHT);
            holder.mTvOptometry3.setGravity(Gravity.LEFT);
            holder.mTvOptometry4.setGravity(Gravity.RIGHT);
            holder.mTvOptometry5.setGravity(Gravity.LEFT);

            holder.mTvOptometry2.setText(R.string.far);
            holder.mTvOptometry3.setText(R.string.user);
            holder.mTvOptometry4.setText(R.string.near);
            holder.mTvOptometry5.setText(R.string.user);
            holder.mView2.setVisibility(View.GONE);
            holder.mView4.setVisibility(View.GONE);
        }
        if (position == 1) {
            holder.mTvOptometry2.setText(R.string.r_eyes);
            holder.mTvOptometry4.setText(R.string.r_eyes);
            holder.mTvOptometry3.setText(R.string.l_eyes);
            holder.mTvOptometry5.setText(R.string.l_eyes);
        }
        if ((position == 3 || position == 6) && isAddData) {
            holder.mTvOptometry2.setFocusable(true);
            holder.mTvOptometry2.setFocusableInTouchMode(true);
            holder.mTvOptometry2.setCursorVisible(true);

            holder.mTvOptometry3.setFocusable(true);
            holder.mTvOptometry3.setFocusableInTouchMode(true);
            holder.mTvOptometry3.setCursorVisible(true);

            holder.mTvOptometry4.setFocusable(true);
            holder.mTvOptometry4.setFocusableInTouchMode(true);
            holder.mTvOptometry4.setCursorVisible(true);

            holder.mTvOptometry5.setFocusable(true);
            holder.mTvOptometry5.setFocusableInTouchMode(true);
            holder.mTvOptometry5.setCursorVisible(true);

            holder.mTvOptometry2.addTextChangedListener(new TextSwitcher(holder, 2));
            holder.mTvOptometry3.addTextChangedListener(new TextSwitcher(holder, 3));
            holder.mTvOptometry4.addTextChangedListener(new TextSwitcher(holder, 4));
            holder.mTvOptometry5.addTextChangedListener(new TextSwitcher(holder, 5));
        } else {
            holder.mTvOptometry2.setFocusable(false);
            holder.mTvOptometry2.setFocusableInTouchMode(false);
            holder.mTvOptometry3.setFocusable(false);
            holder.mTvOptometry3.setFocusableInTouchMode(false);
            holder.mTvOptometry4.setFocusable(false);
            holder.mTvOptometry4.setFocusableInTouchMode(false);
            holder.mTvOptometry5.setFocusable(false);
            holder.mTvOptometry5.setFocusableInTouchMode(false);
        }
        if (position == 7) {
            holder.mView2.setVisibility(View.GONE);
            holder.mView3.setVisibility(View.GONE);
            holder.mView4.setVisibility(View.GONE);
        }
        if (position % 2 == 0) {
            holder.ll_optometry.setBackgroundColor(context.getResources().getColor(R.color.app_dark_blue));
        }
        if (isAddData) {
            holder.mTvOptometry2.setOnClickListener(this);
            holder.mTvOptometry2.setTag(position);
            holder.mTvOptometry3.setOnClickListener(this);
            holder.mTvOptometry3.setTag(position);
            holder.mTvOptometry4.setOnClickListener(this);
            holder.mTvOptometry4.setTag(position);
            holder.mTvOptometry5.setOnClickListener(this);
            holder.mTvOptometry5.setTag(position);
        }
    }


    class TextSwitcher implements TextWatcher {
        private ViewHolder mHolder;
        private int textPosition;

        public TextSwitcher(ViewHolder mHolder, int textPosition) {
            this.mHolder = mHolder;
            this.textPosition = textPosition;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

            int position = 0;
            if (textPosition == 2) {
                position = (int) mHolder.mTvOptometry2.getTag();
            } else if (textPosition == 3) {
                position = (int) mHolder.mTvOptometry3.getTag();
            } else if (textPosition == 4) {
                position = (int) mHolder.mTvOptometry4.getTag();
            } else if (textPosition == 5) {
                position = (int) mHolder.mTvOptometry5.getTag();
            }
            if (position == 3) {
                sanGuang.put(textPosition, s.toString());
            } else if (position == 6) {
                jiaoZheng.put(textPosition, s.toString());
            }
        }
    }

    @Override
    public void onClick(View v) {
        if (itemClick != null) {
            switch (v.getId()) {
                case R.id.tv_optometry_2:
                    itemClick.healthItemClick((Integer) v.getTag(), 2, v);
                    break;
                case R.id.tv_optometry_3:
                    itemClick.healthItemClick((Integer) v.getTag(), 3, v);
                    break;
                case R.id.tv_optometry_4:
                    itemClick.healthItemClick((Integer) v.getTag(), 4, v);
                    break;
                case R.id.tv_optometry_5:
                    itemClick.healthItemClick((Integer) v.getTag(), 5, v);
                    break;
            }
        }
    }

    public interface OnHealthItemClick {
        void healthItemClick(int position, int textPosition, View v);
    }

    class ViewHolder {
        EditText mTvOptometry1;
        TextView mView1;
        EditText mTvOptometry2;
        TextView mView2;
        EditText mTvOptometry3;
        TextView mView3;
        EditText mTvOptometry4;
        TextView mView4;
        EditText mTvOptometry5;
        LinearLayout ll_optometry;
    }
}
